﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.LookAndFeel;
using KNS.Invoice.UI.Settings;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Drawing;
using Invoice.Helper;

namespace KNS.Invoice.UI
{
    public static class KNSMessageBox
    {
        // Fields
        private static bool _AllowCustomLookAndFeel;
        private const MessageBoxButtons DefaultButtons = MessageBoxButtons.OK;
        private const string DefaultCaption = "";
        private const MessageBoxDefaultButton DefaultDefButton = MessageBoxDefaultButton.Button1;
        private const MessageBoxIcon DefaultIcon = MessageBoxIcon.None;
        private const IWin32Window DefaultOwner = null;

        // Methods
        [DllImport("user32.dll")]
        private static extern bool MessageBeep(int uType);
        private static DialogResult[] MessageBoxButtonsToDialogResults(MessageBoxButtons buttons)
        {
            if (!Enum.IsDefined(typeof(MessageBoxButtons), buttons))
            {
                throw new InvalidEnumArgumentException("buttons", (int)buttons, typeof(DialogResult));
            }
            switch (buttons)
            {
                case MessageBoxButtons.OK:
                    return new DialogResult[] { DialogResult.OK };

                case MessageBoxButtons.OKCancel:
                    return new DialogResult[] { DialogResult.OK, DialogResult.Cancel };

                case MessageBoxButtons.AbortRetryIgnore:
                    return new DialogResult[] { DialogResult.Abort, DialogResult.Retry, DialogResult.Ignore };

                case MessageBoxButtons.YesNoCancel:
                    return new DialogResult[] { DialogResult.Yes, DialogResult.No, DialogResult.Cancel };

                case MessageBoxButtons.YesNo:
                    return new DialogResult[] { DialogResult.Yes, DialogResult.No };

                case MessageBoxButtons.RetryCancel:
                    return new DialogResult[] { DialogResult.Retry, DialogResult.Cancel };
            }
            throw new ArgumentException("buttons");
        }

        private static int MessageBoxDefaultButtonToInt(MessageBoxDefaultButton defButton)
        {
            if (!Enum.IsDefined(typeof(MessageBoxDefaultButton), defButton))
            {
                throw new InvalidEnumArgumentException("defaultButton", (int)defButton, typeof(DialogResult));
            }
            MessageBoxDefaultButton button = defButton;
            if (button != MessageBoxDefaultButton.Button1)
            {
                if (button == MessageBoxDefaultButton.Button2)
                {
                    return 1;
                }
                if (button != MessageBoxDefaultButton.Button3)
                {
                    throw new ArgumentException("defaultButton");
                }
                return 2;
            }
            return 0;
        }

        private static Icon MessageBoxIconToIcon(MessageBoxIcon icon)
        {
            if (!Enum.IsDefined(typeof(MessageBoxIcon), icon))
            {
                throw new InvalidEnumArgumentException("icon", (int)icon, typeof(DialogResult));
            }
            switch (icon)
            {
                case MessageBoxIcon.None:
                    return null;

                case MessageBoxIcon.Hand:
                    return SystemIcons.Error;

                case MessageBoxIcon.Question:
                    return SystemIcons.Question;

                case MessageBoxIcon.Exclamation:
                    return SystemIcons.Exclamation;

                case MessageBoxIcon.Asterisk:
                    return SystemIcons.Information;
            }
            throw new ArgumentException("icon");
        }

        public static DialogResult ShowAlert(string text, string caption)
        {
            return Show(text, caption, MessageBoxButtons.OK);
        }

        public static DialogResult ShowYesNoConfirm(string text, string caption)
        {
            return Show(text, caption, MessageBoxButtons.YesNo);
        }

        public static DialogResult ShowOKCancelConfirm(string text, string caption)
        {
            return Show(text, caption, MessageBoxButtons.OKCancel);
        }

        public static DialogResult ShowYesNoCancelConfirm(string text, string caption)
        {
            text = KNSStringHelper.BuildWrapText(text, 80);
            return Show(text, caption, MessageBoxButtons.YesNoCancel);
        }

        public static DialogResult Show(string text)
        {
            return Show((IWin32Window)null, text, "", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(UserLookAndFeel lookAndFeel, string text)
        {
            return Show(lookAndFeel, null, text, "", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(string text, string caption)
        {
            return Show((IWin32Window)null, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(IWin32Window owner, string text)
        {
            return Show(owner, text, "", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(UserLookAndFeel lookAndFeel, string text, string caption)
        {
            return Show(lookAndFeel, null, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(UserLookAndFeel lookAndFeel, IWin32Window owner, string text)
        {
            return Show(lookAndFeel, owner, text, "", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)
        {
            return Show((IWin32Window)null, text, caption, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(IWin32Window owner, string text, string caption)
        {
            return Show(owner, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(UserLookAndFeel lookAndFeel, string text, string caption, MessageBoxButtons buttons)
        {
            return Show(lookAndFeel, null, text, caption, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(UserLookAndFeel lookAndFeel, IWin32Window owner, string text, string caption)
        {
            return Show(lookAndFeel, owner, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            return Show((IWin32Window)null, text, caption, buttons, icon, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons)
        {
            return Show(owner, text, caption, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(UserLookAndFeel lookAndFeel, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            return Show(lookAndFeel, null, text, caption, buttons, icon, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(UserLookAndFeel lookAndFeel, IWin32Window owner, string text, string caption, MessageBoxButtons buttons)
        {
            return Show(lookAndFeel, owner, text, caption, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
        {
            return Show((IWin32Window)null, text, caption, buttons, icon, defaultButton);
        }

        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            return Show(owner, text, caption, buttons, icon, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(UserLookAndFeel lookAndFeel, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
        {
            return Show(lookAndFeel, null, text, caption, buttons, icon, defaultButton);
        }

        public static DialogResult Show(UserLookAndFeel lookAndFeel, IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            return Show(lookAndFeel, owner, text, caption, buttons, icon, MessageBoxDefaultButton.Button1);
        }

        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
        {
            return Show(owner, text, caption, MessageBoxButtonsToDialogResults(buttons), MessageBoxIconToIcon(icon), MessageBoxDefaultButtonToInt(defaultButton), icon);
        }

        public static DialogResult Show(UserLookAndFeel lookAndFeel, IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
        {
            return Show(lookAndFeel, owner, text, caption, MessageBoxButtonsToDialogResults(buttons), MessageBoxIconToIcon(icon), MessageBoxDefaultButtonToInt(defaultButton), icon);
        }

        public static DialogResult Show(IWin32Window owner, string text, string caption, DialogResult[] buttons, Icon icon, int defaultButton, MessageBoxIcon messageBeepSound)
        {
            return Show(null, owner, text, caption, buttons, icon, defaultButton, messageBeepSound);
        }

        public static DialogResult Show(UserLookAndFeel lookAndFeel, IWin32Window owner, string text, string caption, DialogResult[] buttons, Icon icon, int defaultButton, MessageBoxIcon messageBeepSound)
        {
            MessageBeep((int)messageBeepSound);
            KNSMessageBoxForm form = new KNSMessageBoxForm();
            return form.ShowMessageBoxDialog(new XtraMessageBoxArgs(lookAndFeel, owner, text, caption, buttons, icon, defaultButton));
        }

        // Properties
        public static bool AllowCustomLookAndFeel
        {
            get
            {
                return _AllowCustomLookAndFeel;
            }
            set
            {
                _AllowCustomLookAndFeel = value;
            }
        }

    }
}
